'use strict';

const Controller = require('egg').Controller;
const { getCookieValue } = require('../public/processCookies');
/**
* @controller ScriptController 注释必写，swagger-doc是根据这段注释来生成接口的 ）。
*/
class ScriptController extends Controller {
  async list() {
    /**
        * @summary scriptlist
        * @description scriptlist
        * @router get /scriptlist
        * @request query integer limit
        * @request query integer page
        * @request query String oltTypeId
        */
    const params = this.ctx.query;
    const { id: tid } = getCookieValue(this.ctx.cookies, 'token', 'tenant');
    params.tid = tid;
    const result = await this.ctx.service.script.list(params);
    this.ctx.body = {
      msg: '',
      status: 200,
      data: result.rows,
      count: result.count,
    };
  }
  async create() {
    /**
        * @summary scriptcreate
        * @description scriptcreate
        * @router post /scriptcreate
        * @request body scriptcreate value 传入参数
        */
    const params = this.ctx.request.body;
    const { id: tid } = getCookieValue(this.ctx.cookies, 'token', 'tenant');
    params.tid = tid;
    await this.ctx.service.script.create(params);
    this.ctx.body = {
      msg: 'create successful',
    };
  }
  async edit() {
    /**
        * @summary scriptedit
        * @description scriptedit
        * @router post /scriptedit
        * @request body scriptedit value 传入参数
        */
    const params = this.ctx.request.body;
    await this.ctx.service.script.edit(params);
    this.ctx.body = {
      msg: 'edit successful',
    };
  }
  async delete() {
    /**
        * @summary scriptdelete
        * @description scriptdelete
        * @router get /scriptdelete
        * @request query String id
        */
    const params = this.ctx.query;
    await this.ctx.service.script.delete(params);
    this.ctx.body = {
      msg: 'delete successful',
    };
  }
  async singleScript() {
    /**
        * @summary scriptsingle
        * @description scriptsingle
        * @router get /scriptsingle
        * @request query String id
        */
    const params = this.ctx.query;
    const result = await this.ctx.service.script.singleScript(params);
    this.ctx.body = {
      data: result,
    };
  }
}

module.exports = ScriptController;
